package com.ruicar.afs.cloud.afscase.approveocr.controller;

import cn.hutool.core.convert.Convert;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruicar.afs.cloud.afscase.approveocr.condition.ApproveOcrCondition;
import com.ruicar.afs.cloud.afscase.approveocr.service.ApproveOcrBatchService;
import com.ruicar.afs.cloud.afscase.approveocr.service.ApproveOcrDetailService;
import com.ruicar.afs.cloud.afscase.approveocr.vo.ApproveOcrUpdateVO;
import com.ruicar.afs.cloud.afscase.approveocr.vo.ApproveOcrVO;
import com.ruicar.afs.cloud.afscase.approvetask.service.CaseApproveRecordService;
import com.ruicar.afs.cloud.afscase.common.step.StepParam;
import com.ruicar.afs.cloud.afscase.common.utils.StepUtil;
import com.ruicar.afs.cloud.afscase.infomanagement.entity.CaseBaseInfo;
import com.ruicar.afs.cloud.afscase.infomanagement.service.CaseBaseInfoService;
import com.ruicar.afs.cloud.afscase.mq.approvesendinfo.service.ApproveInformInfoService;
import com.ruicar.afs.cloud.afscase.processor.enums.NormalSubmitType;
import com.ruicar.afs.cloud.afscase.remind.entity.CaseRemindDetail;
import com.ruicar.afs.cloud.common.core.enums.AfsEnumUtil;
import com.ruicar.afs.cloud.common.core.exception.AfsBaseException;
import com.ruicar.afs.cloud.common.core.query.QueryCondition;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.modules.afscorebusiness.enums.BusinessStateInEnum;
import com.ruicar.afs.cloud.common.modules.afscorebusiness.enums.CustRoleEnum;
import com.ruicar.afs.cloud.common.modules.afscorebusiness.enums.WhetherEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description OCR识别记录
 * @Author Peng_Jinbo
 * @Date 2020/05/26 17:41
 */
@Slf4j
@Api("影像件")
@RestController
@RequestMapping("/approveOcr")
public class ApproveOcrController {

    @Autowired
    ApproveOcrBatchService approveOcrBatchService;

    @Autowired
    ApproveOcrDetailService approveOcrDetailService;

    @Autowired
    CaseBaseInfoService caseBaseInfoService;

    @Autowired
    ApproveInformInfoService approveInformInfoService;

    @Autowired
    CaseApproveRecordService caseApproveRecordService;

    @GetMapping(value = "/listApproveOcr")
    @ApiOperation("获取所有影像件识别记录")
    public IResponse<IPage<ApproveOcrVO>> listApproveOcr(@ModelAttribute QueryCondition condition) {
        ApproveOcrCondition ocrCondition = JSONObject.parseObject(Convert.toStr(condition.getCondition()), ApproveOcrCondition.class);
        Object json = condition.getCondition();
        if (json.equals("{}")) {
            ocrCondition.setDisposeResult(WhetherEnum.NO.getCode());
            ocrCondition.setOcrResult(WhetherEnum.NO.getCode());
        }
        ocrCondition.setCustRole(CustRoleEnum.MIANCUST.getCode());
        return IResponse.success(approveOcrBatchService.listApproveOcr(
                new Page(condition.getPageNumber(), condition.getPageSize()), ocrCondition));
    }

    @PostMapping(value = "updateApproveOcr")
    @ApiOperation("影像件人工处理")
    public IResponse updateApproveOcr(@RequestBody ApproveOcrUpdateVO updateVO) {
        List<ApproveOcrVO> dataList = updateVO.getDataList();
        if (WhetherEnum.YES.getCode().equals(updateVO.getOperationType())) {
            for (ApproveOcrVO approveOcrVO : dataList) {
                boolean updateResult = caseBaseInfoService.update(
                        Wrappers.<CaseBaseInfo>lambdaUpdate().eq(CaseBaseInfo::getApplyNo, approveOcrVO.getApplyNo())
                                .set(CaseBaseInfo::getBusinessStateIn, AfsEnumUtil.key(BusinessStateInEnum.WAIT_QUERY))
                );
                if (updateResult) {
                    StepUtil.prevSteps(StepParam.builder().applyNo(approveOcrVO.getApplyNo()).build());
                }
            }
        } else {
            ApproveOcrVO approveOcr = dataList.stream().findFirst().orElseThrow(() -> new AfsBaseException("未获取到所选择数据项"));
            CaseRemindDetail remind = new CaseRemindDetail();
            remind.setOperationType(WhetherEnum.YES.getCode());
            remind.setRemindType(AfsEnumUtil.key(NormalSubmitType.SEND_BACK_TO_DEALER));
            remind.setRemindContent(updateVO.getRemark());
            remind.setApplyNo(approveOcr.getApplyNo());
            approveInformInfoService.submitLeaveMessage(remind);
            // 退回后业务状态改为草稿
            caseBaseInfoService.update(
                    Wrappers.<CaseBaseInfo>lambdaUpdate()
                        .eq(CaseBaseInfo::getApplyNo,remind.getApplyNo())
                        .set(CaseBaseInfo::getBusinessStateIn,AfsEnumUtil.key(BusinessStateInEnum.DRAFT))
            );
            approveOcr.setRemark(updateVO.getRemark());
            // 步骤置0，退回重提时重新走OCR识别
            caseBaseInfoService.update(
                    Wrappers.<CaseBaseInfo>lambdaUpdate()
                            .eq(CaseBaseInfo::getApplyNo,approveOcr.getApplyNo())
                            .set(CaseBaseInfo::getWorkflowPrevStep,0)
            );
            caseApproveRecordService.ocrSendBack(approveOcr);
        }
        return IResponse.success(approveOcrBatchService.updateApproveOcr(dataList, updateVO.getRemark(), updateVO.getOperationType(), WhetherEnum.YES));
    }

}
